<template>
	<uni-popup ref="unipopup" type='bottom' round="44rpx" :closeable="true" @maskClick="onClose" backgroundColor="#fff">
		<view class="popupBox">
			<view class="h2 bold">获取您的昵称、头像</view>
			<view class="h3">获取用户头像、昵称，主要用于向用户提供具有辨识度的个人主页、捐资显示等</view>
			<view class="form">
				<view class="input u-flex">
					<view class="h2">头像</view>
					<view class="info">
						<button class="avatar-wrapper" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
							<image mode="aspectFill" class="avatar" :src="avatarUrl || 'https://video.file.zhuochengyun.com/bwc/uploads/20240207/20240207204602905198.jp'"></image>
						</button>

					</view>
					<image class="right-img" src="../static/right.png" mode="aspectFit">
					</image>
				</view>
				<view class="input u-flex">
					<view class="h2">昵称</view>
					<view class="info">
						<input type="nickname" v-model="nickName" placeholder="请输入昵称" @blur="onBlur" />
					</view>
				</view>
			</view>
			<view class="submit" @click="submit">确定</view>
		</view>
	</uni-popup>
</template>
<script>
	import uniPopup from '@/components/uni-popup/uni-popup.vue';
	import {upload} from '@/utils/util.js'
	
	export default {
		components:{uniPopup},
		props: {
			showWxLogin: {
				type: Boolean,
				required: true,
			}
		},
		
		data() {
			return {
				PrimaryColor: '#1fba1a', //主题色
				avatarUrl: '',
				nickName: ''
			}
		},
		methods: {
			open(nickname, avatarUrl) {
				this.nickName = nickname
				this.avatarUrl = avatarUrl
				this.$refs.unipopup.open('bottom')
			},
			onClose() {
				this.$refs.unipopup.close()
			},
			onChooseAvatar(e) {
				uni.compressImage({
					src: e.detail.avatarUrl,
					quality: 80,
					success: res => {
						console.log(res);
						// this.avatarUrl = res.tempFilePath
						this.handleUploadAvatar(res.tempFilePath)
					}
				})
			},
			async handleUploadAvatar(filePath) {
				try {
					upload(filePath).then(res=>{
						console.log('response==',res);
						this.avatarUrl = res
					})
					

				} catch (e) {
					uni.showToast({
						title: '头像上传失败~',
						icon: 'none'
					});
				}
			},
			onBlur(e) {

				this.nickName = e.detail.value
			},
			submit() {
				if (!this.avatarUrl) {
					uni.showToast({
						title: '请上传头像~',
						icon: 'none'
					});
					return
				}
				if (!this.nickName) {
					uni.showToast({
						title: '请输入昵称~',
						icon: 'none'
					});
					return
				}
				let userInfo = {
					avatar_url: this.avatarUrl,
					nick_name: this.nickName,
				}

				this.$emit("setUserInfoCallback", userInfo) //全局登录监听回调方法
			},
		},
	}
</script>
<style lang="scss" scoped>
	.popupBox {
		width: 750rpx;
		box-sizing: border-box;
		padding: 50rpx 40rpx 100rpx;

		.h2 {
			color: #333;
			font-size: 32rpx;
		}

		.h3 {
			font-size: 26rpx;
			color: #999;
			line-height: 1.4;
			padding-top: 14rpx;
		}

		.bold {
			font-weight: bold;
		}

		.form {
			margin-top: 30rpx;
			border-top: 1px solid #EFEFEF;

			.input {
				display: flex;
				padding: 24rpx 0;
				border-bottom: 1px solid #EFEFEF;
				align-items: center;
				justify-content: space-between;

				.info {
					flex: 1;
					margin-left: 32rpx;

					.avatar-wrapper {
						width: 70rpx;
						height: 70rpx;
						border-radius: 10rpx;
						padding-left: 0 !important;
						margin-left: 0 !important;

						.avatar {
							width: 70rpx;
							height: 70rpx;
							border-radius: 10rpx;
						}
					}

					button::after {
						border: none;
					}

					input {
						background: transparent;
						width: 100%;
						height: 70rpx;
						color: #333;
						font-size: 30rpx;
					}
				}

				.right-img {
					width: 24rpx;
					height: 24rpx;
				}
			}
		}
	}

	.submit {
		margin-top: 30rpx;
		width: 100%;
		border-radius: 10rpx;
		text-align: center;
		line-height: 92rpx;
		color: #333;
		font-size: 32rpx;
		height: 88rpx;
		background: linear-gradient(222deg, #fbe542 0%, #fbe542 100%);
		box-shadow: 0px 4rpx 16rpx 0px rgba(0, 0, 0, 0.08);
		border-radius: 44rpx;
	}
</style>